#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"
#include "Comm.hpp"
const std::string gpath = "./";
const std::string gfilename = "dir.txt";
const std::string sep = ":";

using namespace LogModule;

class Dictionaries
{
private:
	// 加载字典
	bool loadDictionaries()
	{
		std::string allpath = _path + _filename;
		std::ifstream ifs(allpath.c_str());
		if (!ifs.is_open())
		{
			LOG(LogLevel::ERROR) << "open file " << allpath << " error";
			return false;
		}

		std::string file;
		while (std::getline(ifs, file))
		{
			std::string left;
			std::string right;
			if (separate(file, &left, &right, sep))
			{
				_dictionaries.insert(std::make_pair(left,right));
			}
		}
		

		ifs.close();
		return true;
	}

public:
	Dictionaries(const std::string &path = gpath, const std::string &filename = gfilename) : _path(path), _filename(filename)
	{
		loadDictionaries();
		Print();
	}
	// 翻译
	std::string translate(const std::string &word)
	{
		auto pos = _dictionaries.find(word);
		if(pos == _dictionaries.end()) return "NONO";
		return pos->second;
	}
	void Print()
    {
        for(auto &item : _dictionaries)
        {
            std::cout << item.first << ":" << item.second<< std::endl;
        }
    }
	~Dictionaries()
	{
	}

private:
	std::unordered_map<std::string, std::string> _dictionaries;
	std::string _path;
	std::string _filename;
};